package cn.edu.ljl;

import java.sql.*;

import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.Statement;



import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;


public class MyDBTest {


    public  static final String INSERT_TEMPLATE="INSERT INTO courses (`nam`, `course`, `advice`) " +
            "VALUES ('%s', '%s', '%s')";

    public static void main(String[] args) {
        String name="ljl";
        String course="MYDB";
        String advice="test";
        //String sql = String.format(INSERT_TEMPLATE, name, course, advice);


        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = MyDB.getConnection();

            String sql = "select * from courses where nam = \"李俊霖\"";

            preparedStatement = connection.prepareStatement(sql);

            //preparedStatement.setString(1,"李俊霖");

            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                System.out.println(resultSet.getString("nam")+" "+resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyDB.close(connection,preparedStatement,resultSet);
        }

        //writeDBByDBCP(sql);
    }

    public static void writeDBByDBCP(String sql){
        try {
            Connection conn = MyDB.getConnection();
            Statement stat = conn.createStatement();
            stat.executeUpdate(sql);
            conn.commit();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

//    public void testWriteDBByEveryConn() throws Exception{
//        for(int i = 0; i < 2000; i++){
//            writeDBByEveryConn(i);
//        }
//        System.out.println("DONE");
//
//    }
//
//    //测试,使用连接池,每写一条数据前,从连接池中获取一个连接
//    public void testWriteDBByDBCP() throws Exception{
//        for(int i = 0; i < 2000; i++){
//            writeDBByDBCP(i);
//        }
//        System.out.println("DONE");
//    }
//
//
//    public void testWriteDBByOneConn() throws Exception{
//        Connection conn = DBConn.getConnection();
//        Statement stat = conn.createStatement();
//        for(int i = 0; i < 2000; i++){
//            writeDBByOneConn(i, stat);
//        }
//        conn.close();
//        System.out.println("DONE");
//    }
//
//    public void writeDBByEveryConn(int data){
//        String sql = "insert into dbcp values (" + data + ")";
//        Connection conn = DBConn.getConnection();
//        try{
//            Statement stat = conn.createStatement();
//            stat.executeUpdate(sql);
//        }catch(Exception e){
//            e.printStackTrace() ;
//        }finally{
//            try {
//                conn.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//
//        }
//    }
//
//    //不使用连接池写数据库,只用一个连接,写所有数据
//    public void writeDBByOneConn(int data, Statement stat){
//        String sql = "insert into dbcp values (" + data + ")";
//        try{
//            stat.executeUpdate(sql);
//        }catch(Exception e){
//            e.printStackTrace() ;
//        }
//    }

    //通过DBCP连接池写数据库





}
